package org.example.myl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.myl.common.Response;
import org.example.myl.common.ResponseCode;
import org.example.myl.entity.Student;
import org.example.myl.entity.aggregation.StudentAggregation;
import org.example.myl.entity.vo.VoStudent;
import org.example.myl.service.StudentService;
import org.example.myl.utils.MinioUtils;
import org.example.myl.zhujie.RateLimit;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController()
@RequestMapping("/student")
@CrossOrigin
public class StudentController {
    @Autowired
    private MinioUtils minioUtils;


    @Autowired
    private StudentService studentService;
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Response insert_student(@RequestBody VoStudent student){
        try {
            studentService.insertstudent(student);
            log.info("添加成功");
            return Response.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } catch (Exception e) {
            log.info("添加失败");
            return Response.builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
    //添加学生，并上传照片
//    @RequestMapping(value = "/insertimages", method = RequestMethod.POST)
//    public Response insert_studentAndImages(@ModelAttribute  VoStudent student ,@RequestParam("images") MultipartFile file){
//        try {
//
//            String imageurl=minioUtils.upload(file);
//            student.setImages(imageurl);
//            studentService.insertstudentandimages(student);
//            log.info("添加成功");
//            return Response.builder()
//                    .code(ResponseCode.SUCCESS.getCode())
//                    .info(ResponseCode.SUCCESS.getInfo())
//                    .build();
//        } catch (Exception e) {
//            log.info("添加失败");
//            return Response.builder()
//                    .code(ResponseCode.UN_ERROR.getCode())
//                    .info(ResponseCode.UN_ERROR.getInfo())
//                    .build();
//        }
//    }
    //根据id删除
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public Response delete_student(@PathVariable Long id){
        try{
            studentService.deletestudent(id);
            log.info("删除成功");
            return Response.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        }catch (Exception e){
            log.info("删除失败");
            return Response.builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
    //查询所有的学生
    @RequestMapping(value = "/selectall", method = RequestMethod.POST)
    public Response<List<Student>> select_student(){
        List<Student> students=studentService.findStudentAll();
        return Response.<List<Student>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(students)
                .build();
    }
    //根据id查询
    @RateLimit(timeWindow = 60, maxRequests = 5)
    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public Response<Student> select_student1(@PathVariable Long id){
        try{
            Student student=studentService.findStudentById(id);
            log.info("根据id查询成功");
            return Response.<Student>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(student)
                    .build();
        }catch (Exception e){
            return Response.<Student>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
    //修改
@RequestMapping(value = "/update", method = RequestMethod.POST)
    public Response update_student(@RequestBody Student student){
        try{
            studentService.updateStudent(student);
            log.info("修改成功");
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        }catch (Exception e){
            log.info("修改成功");
            return Response.<Boolean>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
}
//分页查询
@RequestMapping(value = "/select/{pageNum}/{pageSize}", method = RequestMethod.POST)
    public Response<PageInfo<Student>> page_student(@PathVariable(value = "pageNum")Integer pageNum,
                                          @PathVariable(value = "pageSize")Integer pageSize
                                           ){
    PageHelper.startPage(pageNum, pageSize);
    List<Student> list = studentService.page_student();
    PageInfo<Student> pageInfo = new PageInfo<>(list);
    return Response.<PageInfo<Student>>builder()
            .code(ResponseCode.SUCCESS.getCode())
            .info(ResponseCode.SUCCESS.getInfo())
            .data(pageInfo)
            .build();
}

    @PostMapping("/upload")
    public String upload(MultipartFile file) {
        String url = minioUtils.upload(file);
        log.info("上传成功，url:{}", url);
        return url;
    }
    //根据学生id查询课程查寻课程和分数
    @RequestMapping(value = "/selectcourse", method = RequestMethod.POST)
    public Response<List<StudentAggregation>> selectcourse(@RequestParam Long id){
        List<StudentAggregation> sta=studentService.findStudentCourseAndSourse(id);
        return Response.<List<StudentAggregation>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(sta)
                .build();
    }
    //学生查询课程查寻课程和分数
    @RequestMapping(value = "/findall", method = RequestMethod.POST)
    public Response<List<List<StudentAggregation>>> selectcourse1(){
        List<Student> sta=studentService.findAll();
        //定义一个数组
        List<List<StudentAggregation>> studentAggregations=new ArrayList<>();
        //遍历得到每个id
        for (Student student:sta){
            Long id=student.getId();
            List<StudentAggregation> studentAggregation=studentService.findStudentCourseAndSourse(id);
            studentAggregations.add(studentAggregation);
        }
        return Response.<List<List<StudentAggregation>>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(studentAggregations)
                .build();

    }



}
